import React from 'react';
import {Redirect} from 'react-router-dom';

import styles from './poetryitem.css';
import PictureBox from "../pic/PictureBox";
import PoetryAbsContent from "./PoetryAbsContent";

/**
 * 诗词条目
 */
class PoetryItem extends React.Component {

  state = {
    redirect: false,
  };

  handleClick = () => {
    this.setState({
      redirect: true
    });
  };

  render() {
    const {title, author, content, poetryId, pic, dynasty, from} = this.props;

    // 从搜索页跳转到的详情页时，在url中添加一个from参数
    const fromSearchPageToDetail = from && from === 'search';
    if (this.state.redirect) {
      const now = new Date().getTime();
      const judgeUrl = fromSearchPageToDetail
        ? "/detail?t=" + now + "&pid=" + poetryId + "&from=search"
        : "/detail?t=" + now + "&pid=" + poetryId;
      return (
        <Redirect push to={judgeUrl}/>
      )
    }

    return (
      <div className={styles["poetry-container"]} onClick={this.handleClick}>
        <div className={styles["poetry-content"]}>
          <p className={styles["poetry-title"]}>
            《 {title} 》 <small><sub>&nbsp;&nbsp; {author} </sub></small>
          </p>
          <PoetryAbsContent content={content}/>
        </div>
        <PictureBox item={true} src={pic} text={dynasty}/>
      </div>
    )
  }
}

export default PoetryItem;
